package com.fr.report.script.function;

import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;

public class ROUND extends NormalFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    if (paramArrayOfObject.length < 2)
      return Primitive.ERROR_NAME;
    Object localObject1 = paramArrayOfObject[0];
    Object localObject2 = paramArrayOfObject[1];
    double d1 = 1.0D;
    if ((localObject1 instanceof Number) && (localObject2 instanceof Number))
    {
      double d3;
      int j;
      double d2 = ((Number)localObject1).doubleValue();
      int i = 0;
      if (d2 < 0.0D)
      {
        d2 = Math.abs(d2);
        i = 1;
      }
      if (((Number)localObject2).intValue() < 0)
      {
        for (j = 1; j <= -1 * ((Number)localObject2).intValue(); ++j)
          d1 *= 10.0D;
        if (((Number)localObject1).doubleValue() >= 0.0D)
          d3 = Math.round(d2 / d1) * d1;
        else
          d3 = Math.round(d2 / d1) * d1;
      }
      if (((Number)localObject2).intValue() == 0)
      {
        if (((Number)localObject1).doubleValue() >= 0.0D)
          d3 = Math.round(d2);
        else
          d3 = Math.round(d2);
      }
      else
      {
        for (j = 1; j <= ((Number)localObject2).intValue(); ++j)
          d1 *= 10.0D;
        if (((Number)localObject1).doubleValue() >= 0.0D)
          d3 = Math.round(d2 * d1) / d1;
        else
          d3 = Math.round(d2 * d1) / d1;
      }
      if (i != 0)
        return new Double(-d3);
      return new Double(d3);
    }
    return Primitive.ERROR_NAME;
  }

  public Function.Type getType()
  {
    return Function.MATH;
  }

  public String getCN()
  {
    return "ROUND(number,num_digits):返回某个数字按指定位数舍入后的数字。\nnumber:需要进行舍入的数字。\nnum_digits:指定的位数，按此位数进行舍入。\n如果 num_digits 大于 0，则舍入到指定的小数位。\n如果 num_digits 等于 0，则舍入到最接近的整数。\n如果 num_digits 小于 0，则在小数点左侧进行舍入。\n示例:\nROUND(2.15, 1) 等于 2.2\nROUND(2.149, 1) 等于 2.1\nROUND(-1.475, 2) 等于 -1.48\nROUND(21.5, -1) 等于 20";
  }

  public String getEN()
  {
    return "ROUND(number,num_digits): Rounds a number to a specified number of digits.\nNumber is the number you want to round.\nNum_digits specifies the number of digits to which you want to round number.\n\nRemarks:\n1. If num_digits is greater than 0 (zero), then number is rounded to the specified number of decimal places. \n2. If num_digits is 0, then number is rounded to the nearest integer. \n3. If num_digits is less than 0, then number is rounded to the left of the decimal point. \n\nExample:\n   ROUND(2.15, 1) = 2.2\n   ROUND(2.149, 1) = 2.1\n   ROUND(-1.475, 2) = -1.48\n   ROUND(21.5, -1) = 20";
  }
}